package ctm.javacode;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/fooddemo.do")
public class fooddemo extends HttpServlet {

	private final String SUCCESS_VIEW = "fooddemo.jsp";
	private final String ERROR_VIEW = "commonError.jsp";
	String link = "";
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String ac;
		if (request.isUserInRole("user")){
			ac = (String) session.getAttribute("login");
		}else{
			ac = (String) session.getAttribute("login") + ";" + request.getParameter("id");
		}

		link = request.getParameter( "foodwork" ) ;
		String Selectlink = null;
		switch (link){
		case "1":
			Selectlink = "全穀根莖類";
			break;
		case "2":
			Selectlink = "蔬菜類";
			break;
		case "3":
			Selectlink = "豆魚肉蛋類";
			break;
		case "4":
			Selectlink = "低脂奶類";
			break;
		case "5":
			Selectlink = "水果類";
			break;
		case "6":
			Selectlink = "油脂及堅果種子類";
			break;
		}
		System.out.print(Selectlink+"\n");

		ArrayList<ArrayList<String>> food_NotSuit = new ArrayList<ArrayList<String>>(); // 禁忌食物
		ArrayList<ArrayList<String>> food_Suit = new ArrayList<ArrayList<String>>(); // 推薦匙物

		
		try {
			classificationfood _classificationfood = new classificationfood(ac);
			food_NotSuit = _classificationfood.getnotsuitfood(Selectlink);
			food_Suit = _classificationfood.getsuitfood(Selectlink);
		
			session.setAttribute("Selectlink", Selectlink);
			session.setAttribute("food_NotSuit", food_NotSuit);
			session.setAttribute("food_Suit", food_Suit);

			
			request.getRequestDispatcher(SUCCESS_VIEW).forward(request,
					response);
		}catch (Exception e){
			request.getRequestDispatcher(ERROR_VIEW).forward(request, response);
		}
	}
	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
}
